package com.wsoft.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.exception.Asserts;
import com.wsoft.core.service.RedisService;
import com.wsoft.entity.BaseEntity;
import com.wsoft.system.entity.TsDictEntity;
import com.wsoft.system.manager.DictManager;
import com.wsoft.system.mapper.TsDictMapper;
import com.wsoft.system.query.dict.TsDictQuery;
import com.wsoft.system.query.dict.TsDictReq;
import com.wsoft.system.service.ITsDictDataService;
import com.wsoft.system.service.ITsDictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static java.util.stream.Collectors.groupingBy;

/**
 * <p>
 * 数据字典表 服务实现类
 * </p>
 *
 * @author kyle
 * @since 2024年08月29日 14:37:48
 */
@Service
public class TsDictServiceImpl extends ServiceImpl<TsDictMapper, TsDictEntity> implements ITsDictService {

    @Resource
    private DictManager dictManager;

    @Resource
    private RedisService redisService;



    @Override
    public boolean insert(TsDictReq dto) {
        TsDictEntity entity = Convert.convert(TsDictEntity.class, dto);
        //校验字典编码重复
        List<TsDictEntity> list = dictManager.getDictByCode(entity.getCode());
        if (list.size()>0){
            Asserts.fail("[sys.code.exist]");
        }
        redisService.del(RedisKeyConstant.DICT_CACHE_ALL);
        return save(entity);
    }
    @Override
    public boolean update(TsDictReq dto) {
        TsDictEntity entity = Convert.convert(TsDictEntity.class, dto);
        //原数据
        TsDictEntity dict = getDict(dto.getId());
        //校验字典编码重复
        QueryWrapper<TsDictEntity> query = new QueryWrapper<>();
        query.lambda().eq(TsDictEntity::getCode,entity.getCode())
                .ne(BaseEntity::getId,entity.getId());
        List<TsDictEntity> list = list(query);
        if (list.size()>0){
            Asserts.fail("[sys.code.exist]");
        }
        //清除缓存
        redisService.del(RedisKeyConstant.DICT_CACHE_ALL);
        redisService.del(RedisKeyConstant.DICT_CACHE+dict.getCode());
        return updateById(entity);
    }

    @Override
    public boolean delete(Long id) {
        TsDictEntity dict = getDict(id);
        //校验子字典or删除子字典
        //清除缓存
        redisService.del(RedisKeyConstant.DICT_CACHE_ALL);
        redisService.del(RedisKeyConstant.DICT_CACHE+dict.getCode());
        return  removeById(id);
    }

    @Override
    public TsDictEntity getDict(Long id) {
        return getById(id);
    }

    @Override
    public PageInfo<TsDictEntity> pageDict(TsDictQuery req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        QueryWrapper<TsDictEntity> query = new QueryWrapper<>();
        //字典编码
        if (StrUtil.isNotBlank(req.getCode())) {
            query.lambda().eq(TsDictEntity::getCode, req.getCode());
        }
        //名称
        if (StrUtil.isNotBlank(req.getName())) {
            query.lambda().like(TsDictEntity::getName, req.getName());
        }
        //是否系统字典
        if (req.getIsSystem()!=null) {
            query.lambda().eq(TsDictEntity::getIsSystem, req.getIsSystem());
        }
            query.lambda().orderByAsc(TsDictEntity::getSort);
        return PageInfo.of(list(query));
    }
}
